package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.ModeArrival;

public class ModeArrivalLogic extends AbstractBL{
	public List<ModeArrival> GenerateModeArrival(long hotelId)
	{
		List<ModeArrival> list = new ArrayList<ModeArrival>();
		ModeArrival modeArrival;
		if(GetModeArrival(hotelId,"Owner Vehide")==null){
			modeArrival = new ModeArrival();
			modeArrival.setHotelId(hotelId);
			modeArrival.setTitle("Owner Vehide");
			modeArrival.setDescription("Owner Vehide");
			modeArrival.setAvaliable(true);
			modeArrival.setStatus(true);
			list.add(modeArrival);
		}
		if(GetModeArrival(hotelId,"By Train")==null){
			modeArrival = new ModeArrival();
			modeArrival.setHotelId(hotelId);
			modeArrival.setTitle("By Train");
			modeArrival.setDescription("By Train");
			modeArrival.setAvaliable(true);
			list.add(modeArrival);
		}
		if(GetModeArrival(hotelId,"Tourist Bus")==null){
			modeArrival = new ModeArrival();
			modeArrival.setHotelId(hotelId);
			modeArrival.setTitle("Tourist Bus");
			modeArrival.setDescription("Tourist Bus");
			modeArrival.setAvaliable(true);
			modeArrival.setStatus(true);
			list.add(modeArrival);
		}
		if(GetModeArrival(hotelId,"By Air")==null){
			modeArrival = new ModeArrival();
			modeArrival.setHotelId(hotelId);
			modeArrival.setTitle("By Air");
			modeArrival.setDescription("By Air");
			modeArrival.setAvaliable(true);
			modeArrival.setStatus(true);
			list.add(modeArrival);
		}
		if(GetModeArrival(hotelId,"Rental Car")==null){
			modeArrival = new ModeArrival();
			modeArrival.setHotelId(hotelId);
			modeArrival.setTitle("Rental Car");
			modeArrival.setDescription("Rental Car");
			modeArrival.setAvaliable(true);
			list.add(modeArrival);
		}
		return list;
	}
	public ModeArrival GetModeArrival(long id)
	{
		Key key = KeyFactory.createKey(EntityName.ModeArrival, id);
		try {
			Entity lang = datastore.get(key);
			ModeArrival c = new ModeArrival();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}

	public ModeArrival GetModeArrival(long hotelId,String title)
	{
		Query q = new Query(EntityName.ModeArrival);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetModeArrival(datastore.prepare(q).asSingleEntity());
	}
	
	public List<ModeArrival> GetAllModeArrival(long hotelId)
	{
		Query q = new Query(EntityName.ModeArrival);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("Status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public ModeArrival GetModeArrival(Entity entity)
	{
		if(entity == null) return null;
		ModeArrival item = new ModeArrival();
		item.setEntity(entity);
		return item;
	}
	
	public List<ModeArrival> GetList(List<Entity> entities)
	{
		List<ModeArrival> list = new ArrayList<ModeArrival>();
		for(Entity e : entities)
		{
			ModeArrival a = new ModeArrival();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public ModeArrival AddModeArrival(long hotelId,String title,String description) throws EntityNotFoundException{
		ModeArrival t = new ModeArrival();
		t.setHotelId(hotelId);
		t.setTitle(title);
		t.setDescription(description);
		t.setAvaliable(true);
		t.setStatus(true);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		return t;
	}
	
	public ModeArrival EditModeArrival(long modearrivalId,String title,String description) throws EntityNotFoundException{
		ModeArrival t = GetModeArrival(modearrivalId);
		t.setTitle(title);
		t.setDescription(description);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public ModeArrival DeleteModeArrival(long modearrivalId) throws EntityNotFoundException{
		ModeArrival t = GetModeArrival(modearrivalId);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public ModeArrival ChangeStatus(long modearrivalId) throws EntityNotFoundException{
		
		ModeArrival a = GetModeArrival(modearrivalId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public List<ModeArrival> GetAllModeArrival4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.ModeArrival);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	
	public List<ModeArrival> GetAllModeArrival4Admin(long hotelId)
	{
		Query q = new Query(EntityName.ModeArrival);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("Avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
}
